#include <jni.h>
#include <string>
#include "EncAndDecDef.h"
#include "sstream"

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_a2022090658_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    auto strBuf = EncryptText(hello,"aaaabbbbccccdddd");
    std::string encryptedText = strBuf->str();
    delete strBuf;
    return env->NewStringUTF(encryptedText.c_str());
}



extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_a2022090658_EncAndDec_EncText(JNIEnv *env, jobject thiz, jstring plain_text,
                                               jstring key) {
    const char *plainText = env->GetStringUTFChars(plain_text, nullptr);
    const char *keyText = env->GetStringUTFChars(key, nullptr);
    auto strBuf = EncryptText(std::string(plainText),std::string(keyText));
    env->ReleaseStringUTFChars(key, keyText);
    env->ReleaseStringUTFChars(plain_text, plainText);
    std::string result = strBuf->str();
    delete strBuf;
    return env->NewStringUTF(result.c_str());
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_a2022090658_EncAndDec_DecText(JNIEnv *env, jobject thiz, jstring cipher_Text,
                                               jstring key) {
    const char *cipherText = env->GetStringUTFChars(cipher_Text, nullptr);
    const char *keyText = env->GetStringUTFChars(key, nullptr);
    auto strBuf = DecryptText(std::string(cipherText),std::string(keyText));
    env->ReleaseStringUTFChars(key, keyText);
    env->ReleaseStringUTFChars(cipher_Text, cipherText);
    std::string result(strBuf->begin(), strBuf->end());
    delete strBuf;
    return env->NewStringUTF(result.c_str());
}